package com.kamistoat.meimeimall.meimeimallproduct.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.meimeimallproduct.vo.spuSave.SpuSaveVo;
import com.kamistoat.meimeimall.meimeimallproduct.entity.SpuInfoEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeimall.meimeimallproduct.service.SpuInfoService;
import com.kamistoat.meimeimall.common.utils.R;

/**
 * spu信息
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@Api(tags = "spu信息数据库操作")
@RestController
@RequestMapping("spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */
    @ApiOperation("从数据库获取列表")
    @GetMapping("/list")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageBySpuInfo(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("从数据库查询指定的POJO")
    @GetMapping("/info/{id}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/spu_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:spuinfo:save")
    public R save(@RequestBody SpuSaveVo spuSaveVo){
		spuInfoService.saveSpuInfo(spuSaveVo);

        return R.ok();
    }

    /**
     * 将传来的spuId进行上架：
     * 将spu的状态更改为已上架
     * 将spu下的所有sku的对应信息保存到ES中
     * @param spuId spuId
     * @return R
     */
    @ApiOperation("将指定spu上架到ES")
    @PostMapping("/spu_up_es/{spuId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R spuUPES(@PathVariable("spuId") Long spuId){
        spuInfoService.spuUp(spuId);
        return R.ok();
    }

    /**
     * 将传来的spuId进行下架
     * 将spu状态更改为已下架
     * 将spu下的所有sku信息从ES中删除
     * @param spuId spuId
     * @return
     */
    @ApiOperation("将指定spu从ES删除")
    @DeleteMapping("/spu_delete_es/{spuId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R spuDeleteES(@PathVariable("spuId") Long spuId){
        spuInfoService.spuDown(spuId);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("向数据库更新POJO")
    @PostMapping("/spu_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/spu_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
